package org.mics.organization.controller;

import java.util.List;

import javax.annotation.Resource;

import org.mics.core.page.PageQuery;
import org.mics.core.response.MultipleDataResponse;
import org.mics.core.response.OneDataResponse;
import org.mics.core.response.PageDataResponse;
import org.mics.core.response.SimpleResponse;
import org.mics.jpa.helper.TreeUtils;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.organization.entity.DepartmentDO;
import org.mics.organization.request.DepartmentRequst;
import org.mics.organization.service.CompanyService;
import org.mics.organization.service.DepartmentService;
import org.mics.organization.vo.DepartmentVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 部门
 * @author mics
 * @date 2023-9-22
 * @version  1.0
 */
@RestController
@RequestMapping("/department")
@Tag(name = "部门")
public class DepartmentController {
	@Resource
	private  DepartmentService departmentService;
	@Resource
	private CompanyService companyService;
	
	@Operation(summary =  "增加/修改部门")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
	public SimpleResponse save(@Validated @RequestBody DepartmentRequst departmentRequst ) {
		String id =   departmentService.add(departmentRequst);
		return new OneDataResponse<String>(id);
	}
	
	@Operation(summary =  "删除部门")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public SimpleResponse delete(@PathVariable("id") String id) {
		departmentService.delete(id);
        return new SimpleResponse();
	}
	
	@Operation(summary =  "部门列表")
    @RequestMapping(value = "/listByPage",method = RequestMethod.POST)
	public PageDataResponse<DepartmentVO> listByPage(@RequestBody PageQuery pageQuery) {
		PageDataResponse<DepartmentVO> pageDataResponse =   departmentService.listByPage(pageQuery);
		return pageDataResponse;
	}
	
	@Operation(summary =  "部门列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
	public MultipleDataResponse<DepartmentVO> list(@RequestParam(value = "name",required = false) @Parameter(description = "名称")String name) {
		List<DepartmentDO> departmentDOs =   departmentService.list(name);
		return new MultipleDataResponse<DepartmentVO>(BeanConvertUtil.convertList(departmentDOs, DepartmentVO.class));
	}
	
	@Operation(summary =  "部门树")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
	public OneDataResponse<DepartmentVO> tree() {
		List<DepartmentDO> departmentDOs =  departmentService.list(null);
		List<DepartmentVO> departmentVOs = BeanConvertUtil.convertList(departmentDOs, DepartmentVO.class);
		return new OneDataResponse<DepartmentVO>(TreeUtils.convertOneTree(departmentVOs));
	}
}
